#coding=utf-8
"""author: wenyao
   data: 2021/11/30
   project: cjh
"""
#1、正则表达式作用？
#a、文本过滤
#b、合法性验证

# import re
# ##############查找匹配############
# #search  match
# #search和match 是查找第一个匹配
# #match是从头部字符串开始查找，只匹配字符串的开始
# #
# # >>> result = re.search("sanchuang", "hello,world this is sanchuang")
# # >>> result
# # <_sre.SRE_Match object; span=(20, 29), match='sanchuang'>
# # >>> type(result)
# # <class '_sre.SRE_Match'>
# # >>> result
# # <_sre.SRE_Match object; span=(20, 29), match='sanchuang'>
# # >>> result = re.search("sanchuang", "hello,world this is sanchu")
# # >>> result
# # >>> print(result)
# # None
# # >>> re.search("sanchuang", "hello,world this is sanchu")
# # >>> re.search("sanchuang", "hello,world this is sanchuang sanchuang")
# # <_sre.SRE_Match object; span=(20, 29), match='sanchuang'>
# # >>> re.match("sanchuang", "hello,world this is sanchuang sanchuang")
# # >>> re.match("sanchuang", "sanchuang hello,world this is sanchuang sanchuang")
# # <_sre.SRE_Match object; span=(0, 9), match='sanchuang'>
# # >>> re.match("sanchuang", "sanchuangsanchuang hello,world this is sanchuang sanchuang")
# # <_sre.SRE_Match object; span=(0, 9), match='sanchuang'>
#
# #r标志位   表示的是输出原始字符串（抑制转义）
# #正则表达式，最好使用原始字符串
# # >>> re.search(r"\\\\tsanle", "hello\\\\tsanle")
# # <_sre.SRE_Match object; span=(5, 13), match='\\\\tsanle'>
# # >>> re.search("\\\\tsanle", "hello\\\\tsanle")
# # <_sre.SRE_Match object; span=(6, 13), match='\\tsanle'>
# # >>> re.search(r"\\\\tsanle", "hello\\\\\tsanle")
# # >>> re.search("\\\sanle", "hello\\\\\tsanle")
# # >>> re.search("\\\sanle", "hello\\\\\sanle")
# # <_sre.SRE_Match object; span=(7, 13), match='\\sanle'>
# # >>> print("hello\\\\\sanle")
#
# #match对象 （通过search和match方法查找出来的结果就是一个match对象）
# # msg = "It's raining cats and dogs"
# # result = re.search(r"cats",msg)
# # print(result)
# # print(result.group())
# # print(result.start())
# # print(result.end())
#
# #findall 和finditer
# #findall 找到所有匹配项 返回一个列表
# # msg = "It's raining cats and dogs, cats1 cats2"
# # result = re.findall("cats", msg)
# # print(result)
#
# #查找里面匹配到的每一项，并且每一项封装成match对象放在一个迭代对象里返回
# # result2 = re.finditer("cats", msg)
# # for i in result2:
# #     print(i.group())
#
# #正则替换
# #re.sub('匹配正则','替换内容','string')
# # print(re.sub("python","PYTHON","I am learning python  python"))
#
# #正则编译
# # msg1 = "hell world"
# # msg2 = "i am learning python"
# # msg3 = "sanle"
# # print(re.findall("python",msg1))
# # print(re.findall("python",msg2))
# # print(re.findall("python",msg3))
# #
# # #slice
# # reg = re.compile("python")
# # print(reg.findall(msg1))
# # print(reg.findall(msg2))
# # print(reg.findall(msg3))
#
# ############基本正则
# #区间 []  可以编码顺序来规定范围
# # ret = re.findall("python", "Python or python")
# # print(ret)
# # ret = re.findall("[pPfg]ython", "PythonorpythonfpythonythonFythonfython")
# # print(ret)
# # ret = re.findall("[A-Za-z-]","abc123AbCD--")
# # print(ret)
# # ret = re.findall("[A-Za-z0-9]","abc123AbCD--")
# # print(ret)
# # ret = re.findall("[A-Za-z0-9]c","abc123AbCD--")
# # print(ret)
#
# #区间取反
# # ret = re.findall("[^A-Z]c","Ac111crc#c")
# # print(ret)
# # ret = re.findall("[^A-Z][0-9]","Ac111crc#c")
# # print(ret)
#
# #匹配或
# # msg = "welcom to changsha, welcom to hunan"
# # ret = re.findall("changsha|hunan", msg)
# # print(ret)
#
# #. 占位符， 表示除\n以外的任意一个字符
# # ret = re.findall("p.thon","Pythonpyython p thon p-thon p\nthon")
# # print(ret)
#
# #匹配开始与结束
# #^, $
# # ret = re.findall("^python", "python, hello, python")
# # print(ret)
# # ret = re.findall("python$", "python, hello, python")
# # print(ret)
#
# #快捷方式
# #\A \b \B \w \W \d \D \s \S
# # ret = re.findall(r"\d\d","ab1c3450")
# # print(ret)
# # ret = re.findall(r"\w","a3%67.三low_-")
# # print(ret)
# # ret = re.findall(r"\bworld\b", "world helloworld, world 123world world123 #world")
# # print(ret)
#
# #########正则重复
# #通配符  *   ？  +
# # #？  表示匹配前一项0次或一次
# # ret = re.findall("py?","python p pyy ps ")
# # print(ret)
# # # + 表示匹配前一项一次以上
# # ret = re.findall("py+", "python p pyy ps")
# # print(ret)
# # # * 表示匹配前一项任意次（0-n次）
# # ret = re.findall("py*", "python p pyy ps")
# # print(ret)
# # #{n,m}  表示匹配前一项n-m次
# # ret = re.findall("py{2,4}", "python p pyy ps pyyyy")
# # print(ret)
# # #{n}  表示匹配n次
# # ret = re.findall("py{2}","python pyy")
# # print(ret)
#
# #练习：
# #接收用户从键盘的输入
# #如果用户输入的是正整数那就打印输出正整数
# #判断是正整数 还是 负整数 还是浮点数  还是其他
#
# # while 1:
# #     num = input("please input your number:")
# #     #+00001 00001 +01 +100 100 +0000000
# #     if re.findall("^\+?0*[1-9]\d*$",num):
# #         print(f"{num}是正整数")
# #     elif re.findall("^-0*[1-9]\d*$",num):
# #         print(f"{num}是负整数")
# #     elif re.findall("^[+-]?\d+\.\d+$",num):
# #         print(f"{num}是浮点数")
# #     else:
# #         print("其他")
#
# #贪婪模式和非贪婪模式
# #贪婪模式， 尽可能匹配长的字符串
# #非贪婪模式， 匹配到就输出  (+?  *?  {2,4}?)
# # msg = "hellooooooooo"
# # print(re.findall("lo{3,}?",msg))
# # msg = "cats and dogs, cats1 and dog1"
# # print(re.findall("cats.*s",msg))
# # print(re.findall("cats.*?s",msg))
#
# #1-255
# # while True:
# #     num = input("请输入1-255之间的数：")
# #     if re.match("^[1-9]\d?$|^1\d{2}$|^2[0-4]\d$|^25[0-5]$",num):
# #         print("是")
# #     else:
# #         print("否")
#
# #######正则分组
# #match对象的group函数 默认参数为0， 表示输出匹配的所有字符串
# #                   参数n（n>0）,表示输出第几个分组匹配到的内容
# # msg = "tel:173-7572-2991"
# # ret = re.search(r'(\d{3})-(\d{4})-(\d{4})',msg)
# # print(ret.group())
# # print(ret.group(1))
# # print(ret.group(2))
# # print(ret.groups())
#
# #分组向后引用
# #捕获分组
# #(regex)
# # -- 分组之后匹配到的数据都是暂时放在内存里，并且给定一个从1开始的索引
# #            所以，捕获分组是可以向后引用 \1 \2
# # ret = re.search(r'(\d{3})-(\d{4})-\2',"173-7572-7572")
# # print(ret.group())
# # ret = re.search(r'(\d{3})-(\d{4})-\1',"173-7572-173")
# # print(ret.group())
# #
# # #非捕获分组
# # #(?:regex)
# # #只分组不捕获， 不会将匹配到的内容临时存放在内存，不能使用分组向后引用
# # ret = re.search(r'(?:\d{3})+-(\d{4})-\1',"173-7572-7572")
# # print(ret.group(1))
# #
# # #如果有捕获分组，findall只会匹配捕获分组内容
# # ret = re.findall(r'(?:\d{3})+-(\d{4})-\1',"173-7572-7572")
# # print(ret)
#
# #作业：
# # 1、msg="comaa@126.comyy@bb.comcombb@qq.comxx@163.com"
# # 找出126.com和qq.com和163.com的邮箱地址
# # msg="comaa@126.comyy@bb.comcombb@qq.comxx@163.com"
# # print(re.findall(r"(?:\.com)?(\w+@(?:126|qq|163)\.com)",msg))
#
# #既要匹配，又不想显示出结果，那就活用findall捕获分组和非捕获分组
# # ((3+3)*3+4)
#
# #提取域名地址
# #http://www.baidu.com
# #https://hu.www.baidu.com
# #http://baidu.com?xx=a
# #https://baidu.com/aa
# #this is test
# #x.x.xx
#
# # ret = re.findall("^https?://((?:\w+\.)+\w+)[?/]?","http://www.baidu.com=")
# # print(ret)
# # ret = re.findall("^https?://((?:\w+\.)+\w+)$|^https?://((?:\w+\.)+\w+)[?/]","http://www.baidu.com=")
# # print(ret)
# #
# # #验证用户名（以字母开头，是字母、数字、下划线的任意组合， 8-10位）
# # print(re.findall("^[A-Za-z][a-zA-Z0-9_]{7,9}","abc_1234"))
# # #
# # msg = "i am learning Eglish, welcom to 'chang sha', test msg"
# # print(re.findall("([A-Za-z]+)|(?:\')(.*)(?:\')",msg))
#
# #分组向后引用
# #正则标记
# #re.I 大小写不明感
# #re.M 多行匹配
# #re.S 使.表示包括换行符在内的任意字符
#
# # msg = '''
# # python
# # PyThon
# # '''
# # print(re.findall('^python$',msg,re.M|re.I))
# # print(re.findall('.+',msg,re.S))
#
# import re
#
# msg = '17375722991fdgdfsg13511111111sakldaskjdhf+8613333333333kjhds+86133333333333333333333333fhhh17375722991'
# # print(re.findall(r"^((?:\+\d{2,3})?1\d{10})\D|\D((?:\+\d{2,3})?1\d{10})\D|\D((?:\+\d{2,3})?1\d{10})$", msg))
# print(re.findall(r"(?<!\d)((?:\+\d{2,3})?1\d{10})(?!\d)", msg))

# import re
# #零宽断言
# msg = "i am learning Eglish, welcom to 'chang sha', test msg"
# print(re.findall("(?<=\').*(?=\')|[A-Za-z]+",msg))
#
# msg = '''
# 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
#     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
#     inet 127.0.0.1/8 scope host lo
#        valid_lft forever preferred_lft forever
#     inet6 ::1/128 scope host
#        valid_lft forever preferred_lft forever
# 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
#     link/ether 00:0c:29:c7:14:52 brd ff:ff:ff:ff:ff:ff
#     inet 192.168.149.132/24 brd 192.168.149.255 scope global noprefixroute ens33
#        valid_lft forever preferred_lft forever
#     inet6 fe80::b22e:e2c4:2912:55b8/64 scope link noprefixroute
#        valid_lft forever preferred_lft forever
# '''
# print(re.findall(r"(?<=inet ).*(?=/)",msg))

#ip地址
#ipv4   0-255.0-255.0-255.0-255
#爬取三创官网上的图片
#img标签里的src属性后面接的路径就是图片路径
#src="/upload/links/momo.jpg" -->https://sanchuangedu.cn/upload/links/momo.jpg
#拿到图片地址之后进行访问  requests
#然后下载到本地  open
import re

# while 1:
#     msg=input("请输入ip地址：")
#     ret=re.search(r"((1?\d?\d|2[0-4]\d|25[0-5])\.){3}(25[0-5]|2[0-4]\d|[1]?\d?\d)",msg)
#     print(ret.group())

import requests
url="https://www.baidu.com/"
url1="https://www.baidu.com"
y=requests.get(url)
print(y.text)
ret=re.findall(r"(?<=<img src=)\S+.(?:jpg|gif)",y.text)
print(ret)
x=0
for i in ret:
    ret1=i.strip("//")
    ret2=requests.get("http://"+ret1)
    with open("img{}.jpg".format(x),"wb") as f:
         f.write(ret2.content)
         # print(ret2)
         x=x+1